---
title: "Regression analysis - Hebrew"
author: "`r Sys.info()['user']`"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
require(memisc)
source("functions.R")
source("models.R")
dat <- cbind(readRDS("data_lss_he.RDS"), 
             readRDS("data_newsmap_he.RDS"), 
             readRDS("data_dictionary_he.RDS"))
dat <- subset(dat, class %in% c("ir", "il") & date <= as.Date("2018-12-31"))
events <- yaml::read_yaml("events.yml")

window <- 30
dat <- flag_events(dat, events$IL, window)
dat2 <- flag_events(dat, events$IL, window, merge = FALSE)
```

## Variables 

- publication: Haarez or Yedioth
- class: article's focus to Israel or Iran
- keywords (see dictionary_he.yml):
    - government: mention of prime minister or his cabinet members
- evens (see events.yml)
    - election: within `r window` days before elections
    - legislation: within `r window` days before security legislation
    - military: within `r window` days after major military events
    - diplomacy: within `r window` days after major diplomatic events

## Models

```{r results="asis"}
mtable("Model 0" = lm(f0, dat), 
       "Model 1" = lm(f1, dat),
       "Model 2" = lm(f2, dat), 
       "Model 3" = lm(f3, dat), 
       "Model 4" = lm(f4, dat),
       "Model 5" = lm(f5, dat),
       show.baselevel = FALSE,
       summary.stats = c("sigma", "R-squared", "F", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?" = "military (M)", 
            "diplomacy(: )?" = "diplomacy (D)", 
            "election(: )?" = "election (E)", 
            "legislation(: )?" = "legislation (L)", 
            gsub = TRUE) %>%
    show_html()
```

```{r results="asis"}
mtable("Model 2" = lm(f2, dat2),
       "Model 6" = lm(f3, dat2),
       "Model 7" = lm(f5, dat2), 
       show.baselevel = FALSE,
       coef.style = "horizontal",
       summary.stats = c("sigma", "R-squared", "F", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?(\\d+)" = "military (M\\2)", 
            "diplomacy(: )?(\\d+)" = "diplomacy (D\\2)", 
            "election(: )?(\\d+)?" = "election (E\\2)", 
            "legislation(: )?(\\d+)?" = "legislation (L\\2)", 
            gsub = TRUE) %>%
    show_html()
```

```{r results="asis"}
dat3 <- subset(dat, class == "ir")
mtable("Model 8" = glm(f7, dat3, family = binomial), 
       "Model 9" = glm(f8, dat3, family = binomial),
       show.baselevel = FALSE,
       getSummary = getSummary_expcoef,
       coef.style = "horizontal",
       summary.stats = c("Log-likelihood", "AIC", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?" = "military (M)", 
            "diplomacy(: )?" = "diplomacy (D)", 
            "election(: )?" = "election (E)", 
            "legislation(: )?" = "legislation (L)", 
            gsub = TRUE) %>%
    show_html()
```

```{r}
t.test(lss ~ alone, dat3)
```

## Predicted values

```{r fig.height=5, fig.width=10}
dat_dummy <- expand.grid(
    publication = unique(dat2$publication),
    class = c("ir", "il"), government = FALSE, 
    alone = c(TRUE, FALSE),
    election = factor(1:3),
    legislation = factor(0),
    military = factor(0),
    diplomacy = factor(0)
)
pred <- predict(lm(f5, dat2), newdata = dat_dummy, interval = "confidence", level = 0.9)
dat_dummy <- cbind(dat_dummy, pred)
par(mfrow = c(1, 2), cex = 1.0)
ylim <- c(-0.8, 0.8)
plot_prediction(subset(dat_dummy, publication == "haaretz" & class == "ir" & !alone), col = color[2],
                var = "election", label = "Electoral", main = "Iran",
                xlim = c(0.7, 3.2), ylim = ylim)
plot_prediction(subset(dat_dummy, publication == "yedioth" & class == "ir" & !alone), col = color[1], add = TRUE,
                var = "election", label = "Electoral")
legend("topleft", c("Haaretz", "Yedioth"), 
       col = color[2:1], lty = 1, lwd = 2, horiz = TRUE)
plot_prediction(subset(dat_dummy, publication == "haaretz" & class == "ir" & alone), col = color[2],
                var = "election", label = "Electoral", main = "Only Iran",
                xlim = c(0.7, 3.2), ylim = ylim)
plot_prediction(subset(dat_dummy, publication == "yedioth" & class == "ir" & alone), col = color[1], add = TRUE,
                var = "election", label = "Electoral")
legend("topleft", c("Haaretz", "Yedioth"), 
       col = color[2:1], lty = 1, lwd = 2, horiz = TRUE)
```


## Multi-colinear

```{r}
cor(dat[,c("election", "legislation", "military", "diplomacy")])
```

```{r fig.height=2, fig.width=10}
plot_dummy(events$IL, window)
```

